package com.jhgsys.internal.incentive.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClause;
import com.jhgsys.internal.incentive.entity.IncentiveRewardClauseRule;
import com.jhgsys.internal.incentive.service.IIncentiveRewardClauseService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 奖励条款 Controller
 *
 * @author zjb
 * @date 2024-08-30 09:51:04
 */
@Slf4j
@Validated
@Controller
public class IncentiveRewardClauseController extends BaseController {

    @Autowired
    private IIncentiveRewardClauseService incentiveRewardClauseService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClause")
    @RequiresPermissions("incentiveRewardClause:view")
    public String incentiveRewardClauseIndex(Model model){
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("incentive/incentiveRewardClause/incentiveRewardClauseList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClause/add")
    @RequiresPermissions("incentiveRewardClause:add")
    public String incentiveRewardClauseAdd(){
        return FebsUtil.view("incentive/incentiveRewardClause/incentiveRewardClauseEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardClause/update/{id}")
    @RequiresPermissions("incentiveRewardClause:edit")
    public String incentiveRewardClauseUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("incentiveRewardClause", incentiveRewardClauseService.findIncentiveRewardClauseById(id));
        return FebsUtil.view("incentive/incentiveRewardClause/incentiveRewardClauseEdit");
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-08-30 09:51:04
     */
    @GetMapping("incentiveRewardClause")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:view")
    public FebsResponse getAllIncentiveRewardClauses(IncentiveRewardClause incentiveRewardClause) {
        return new FebsResponse().success().data(incentiveRewardClauseService.findIncentiveRewardClauseList(incentiveRewardClause));
    }

    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-08-30 09:51:04
     */
    @GetMapping("incentiveRewardClause/list")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:view")
    public FebsResponse incentiveRewardClauseList(QueryRequest request, IncentiveRewardClause incentiveRewardClause) {
        Map<String, Object> dataTable = getDataTable(this.incentiveRewardClauseService.findIncentiveRewardClausePage(request, incentiveRewardClause));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveRewardClause
     * @author zjb
     * @date 2024-08-30 09:51:04
     */
    @Log("新增IncentiveRewardClause")
    @PostMapping("incentiveRewardClause")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:add")
    public FebsResponse addIncentiveRewardClause(@RequestBody @Valid IncentiveRewardClause incentiveRewardClause) throws FebsException {
        try {
            IncentiveRewardClause clause = new IncentiveRewardClause();
            clause.setTenantId(incentiveRewardClause.getTenantId());
            clause.setClauseName(incentiveRewardClause.getClauseName());
            List<IncentiveRewardClause> incentiveRewardClauseList = this.incentiveRewardClauseService.findIncentiveRewardClauseList(clause);
            if(!CollectionUtils.isEmpty(incentiveRewardClauseList) && !incentiveRewardClauseList.get(0).getClauseId().equals(incentiveRewardClause.getClauseId())){
                return new FebsResponse().fail().message("该条款名称已存在！");
            }
            incentiveRewardClause.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveRewardClauseService.createIncentiveRewardClause(incentiveRewardClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveRewardClause失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveRewardClause
     * @author zjb
     * @date 2024-08-30 09:51:04
     */
    @Log("逻辑删除IncentiveRewardClause")
    @GetMapping("incentiveRewardClause/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:delete")
    public FebsResponse deleteIncentiveRewardClause(@PathVariable("id") String incentiveRewardClauseIds) throws FebsException {
        try {
            IncentiveRewardClause incentiveRewardClause = new IncentiveRewardClause();
            incentiveRewardClause.setDeletedBy(getCurrentUser().getUserName());
            incentiveRewardClause.setDeletedTime(new Date());
            incentiveRewardClause.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveRewardClauseService.deleteIncentiveRewardClause(incentiveRewardClause,incentiveRewardClauseIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardClause失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 IncentiveRewardClause
    * @author zjb
    * @date 2024-08-30 09:51:04
    */
    @Log("物理删除IncentiveRewardClause")
    @GetMapping("incentiveRewardClause/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:delete")
    public FebsResponse physicallyDeleteIncentiveRewardClause(@PathVariable("id") String incentiveRewardClauseIds) throws FebsException {
        try {
            this.incentiveRewardClauseService.physicallyDeleteIncentiveRewardClause(incentiveRewardClauseIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardClause失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 IncentiveRewardClause
     * @author zjb
     * @date 2024-08-30 09:51:04
     */
    @Log("修改IncentiveRewardClause")
    @PostMapping("incentiveRewardClause/update")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:edit")
    public FebsResponse updateIncentiveRewardClause(@RequestBody @Valid IncentiveRewardClause incentiveRewardClause) throws FebsException {
        try {
            IncentiveRewardClause clause = new IncentiveRewardClause();
            clause.setTenantId(incentiveRewardClause.getTenantId());
            clause.setClauseName(incentiveRewardClause.getClauseName());
            List<IncentiveRewardClause> incentiveRewardClauseList = this.incentiveRewardClauseService.findIncentiveRewardClauseList(clause);
            if(!CollectionUtils.isEmpty(incentiveRewardClauseList) && !incentiveRewardClauseList.get(0).getClauseId().equals(incentiveRewardClause.getClauseId())){
                return new FebsResponse().fail().message("该条款名称已存在！");
            }
            incentiveRewardClause.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveRewardClauseService.updateIncentiveRewardClause(incentiveRewardClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveRewardClause失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("修改条款类型状态")
    @PostMapping("incentiveRewardClause/updateStatus")
    @ResponseBody
    @RequiresPermissions("incentiveRewardClause:edit")
    public FebsResponse updateBaseStatus(@RequestBody IncentiveRewardClause incentiveRewardClause) throws FebsException {
        try {
            incentiveRewardClause.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveRewardClauseService.updateIncentiveRewardClause(incentiveRewardClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改条款类型失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
